Update and Add Data

Check If Record Already Exists Before Saving Data

Description
This customization shows how to verify if a record already exists when inserting a new record.
Variables
RecordControl
Select a record control
Table Name
Select a table to insert record
Primary Key
Select the primary key in the table
Primary Key Control
Select the primary key control in the table
Applies to
RecordControl class
Code
 
/// 
/// This sub is called when a record is getting saved. Call base.SaveData()
/// only if duplicate record is not present in the database.
/// 
public override void SaveData()
{
    string whereStr = ${${Table Name}ClassName}.${Primary Key}.UniqueName + " = '" + this.${Primary Key Control}.Text + "'";

    // Check if record already exists before saving.
    ${${Table Name}RecordClassName}  rec = ${${Table Name}ClassName}.GetRecord(whereStr);
    if (rec != null)
    {   
        // If record exists, do not save the record       
    }
    else
    {      
        // Save the record if it does not exists
        base.SaveData();
    }
}
 

Terms of Service Privacy Statement